﻿@using SelfMadeMillionaire.Core
@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Publications
@using PagedList.Mvc
@model PublicationListViewModel

<div class="table-responsive">
	@if (Model.Publications.Any())
 {
		<table class="table table-bordered tablesorter">
			<thead>
				<tr>
					<th><a href="#">Date</a></th>
					<th><a href="#">Product</a></th>
					<th><a href="#">Title</a></th>
					<th><a href="#">Home Page</a></th>
					<th><a href="#">Sent On</a></th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var pub in Model.Publications)
	{
					<tr>
						<td>@pub.Date.ToString("MM/dd/yyyy")</td>
						<td>@ProductTypeFormatter.GetProductTypeName((ProductType)Enum.Parse(typeof(ProductType), pub.ProductType.ToString()))</td>
						<td>
							@if (String.IsNullOrEmpty(@pub.ExternalUrl))
							{
								<a href="@Url.Action("Details", "Publications", new { id = pub.ID, area = "" })">@pub.Title</a>
							}
							else
							{
								<a href="@pub.ExternalUrl" target="blank">@pub.Title</a>
							}
						</td>
						<td>@pub.IsShowOnHomePage</td>
						<td>@Html.Raw(DateSentFormatter.FormatSentDate(pub.EmailMessage))</td>
						<td>
							@if (!string.IsNullOrWhiteSpace(pub.PdfFileName))
	   {
								<text>
								<a href="@FileLink.GetAbsoluteLinkOnFile(pub.PdfFileName)" target="_blank">View PDF</a>
								</text>
	   }

                            @if (pub.EmailMessage == null)
                            {
                                <text>
                                | <a href="@Url.Action("Edit", "Publications", new { id = pub.ID })" class="edit">Edit</a>
                                | <a href="#" class="remove-lnk" data-id="@pub.ID">Delete</a>
                                | <a href="#" class="send-lnk" data-id="@pub.ID">Send</a>
							    </text>
	                        }
                            else
                            {
                                if (pub.IsShowOnHomePage)
                                {
                                    <text>
                                    | <a href="javascript:void(0);" class="hide-lnk" data-id="@pub.ID">Hide on product page</a>
                                    </text>
                                }
                                else
                                {
                                    <text>
                                    | <a href="javascript:void(0);" class="show-lnk" data-id="@pub.ID">Show on product page</a>
                                    </text>
                                }
                            }
						</td>
					</tr>
	}
			</tbody>
		</table>
		@Html.PagedListPager(Model.Publications, page => Url.Action("Index", new { page, startDate = Model.StartDate, endDate = Model.EndDate, selectedProduct = Model.ProductTypes.SelectedValue, title = Model.Title, isEd = Model.IsEducation?1:0, isHP = Model.IsShowOnHomePage?1:0, minAccessLevel = Model.AccessLevels.SelectedValue, sort = Model.SortOrder}))
 }
 else
 {
		<p>No records found.</p>
 }
</div>

<script type="text/javascript">
	$(function () {
	    $("#publications-list table").tablesorter({
	        headers: {
	            5: { sorter: false }
	        }
	    });

	    $(".remove-lnk").click(function () {
	        if (confirm('Are you sure you want to delete this publication?')) {
	            $.ajax({
	                url: '@Url.Action("Delete", "Publications")',
				    type: "POST",
				    data: { id: $(this).attr('data-id') },
				    cache: false,
				    success: function () {
				        GetList();
				    }
				});
            }
            return false;
	    });

	    $(".send-lnk").click(function ()
	    {
	        @if(Model.IsSendLinkVisible)
            {
                <text>

	            if (confirm('Are you sure to send this publication?')) {
	                $.ajax({
	                    url: '@Url.Action("Send", "Publications")',
	                    type: "POST",
	                    data: { id: $(this).attr('data-id') },
	                    cache: false,
	                    success: function () {
	                        GetList();
	                    }
	                });
	            }
	        </text>
            }
         else
         {
            <text>
	        alert("Only US users can send publications");
            </text>             
         }

            return false;
	    });

	    $(".hide-lnk").click(function () {
	        if (confirm('Are you sure to hide this publication?')) {
	            $.ajax({
	                url: '@Url.Action("Hide", "Publications")',
	                type: "POST",
	                data: { id: $(this).attr('data-id') },
	                cache: false,
	                success: function () {
	                    GetList();
	                }
	            });
            }
            return false;
	    });

	    $(".show-lnk").click(function () {
	        if (confirm('Are you sure to show this publication?')) {
	            $.ajax({
	                url: '@Url.Action("Show", "Publications")',
	                type: "POST",
	                data: { id: $(this).attr('data-id') },
	                cache: false,
	                success: function () {
	                    GetList();
	                }
	            });
            }
            return false;
        });
        
		$("a.edit").click(function () {
			var startDate = $("#@Html.IdFor(m => m.StartDate)").val();
			var endDate = $("#@Html.IdFor(m => m.EndDate)").val();
			var selectedProduct = $("#@Html.IdFor(m => m.SelectedProduct)").val();
		    var title = $("#@Html.IdFor(m => m.Title)").val();
		    var page = '@(Model.Publications.PageNumber.ToString())';

		    var isEd = "0";
		    if ($("#@Html.IdFor(m => m.IsEducation)").prop('checked'))
		        isEd = "1";

		    var isHP = "0";
		    if ($("#@Html.IdFor(m => m.IsShowOnHomePage)").prop('checked'))
		        isHP = "1";

		    var minAccessLevel = $("#@Html.IdFor(m => m.MinAccessLevel)").val()
		    var sort = $("#@Html.IdFor(m => m.SortOrder)").val();

		    $(this).attr("href", $(this).attr("href") + "?startDate=" + startDate + "&endDate=" + endDate + "&selectedProduct=" + selectedProduct + "&title=" + title + "&isEd=" + isEd + "&isHP = " + isHP + "&minAccessLevel=" + minAccessLevel + "&sort=" + sort + "&page=" + page);
		});

	    var sortRefresh = true;
	    @if (!string.IsNullOrEmpty(Model.SortOrder))
     {
            <text>
	        sortRefresh = false;
	        setInitSort("publications-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#publications-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            GetList();
	        else
	            sortRefresh = true;

	    });
	});
</script>
